// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
	let cmd=cloud.database().command;

  let res= await cloud.database().collection("cityRank").where({
    city:event.city
  }).orderBy("star", "desc").limit(30).get();  
  if(res.data.length===0)
  {
  	//add new city record
  	await cloud.database().collection("cityRank").add({data: {city:event.city, star:event.val} });
  	return {updateRet:"ok"};

  }
  else
  {
  	//更新
  	await cloud.database().collection("cityRank").where({city:event.city}).update({data: {star:cmd.inc(event.val)} });
  	return {updateRet:"ok"};
  }

  
}